//max heap
public class Heap {
	final in STARTSIZE = 42;
	int[] data;
	int last;

	public Heap() {
		data = new int[STARTSIZE];
		last = 0;
	}

	public void add(int target) {
		data[++last] = target;
		int current = last;

		while(current > 1) {

			if(data[current/2] > data[current])
				return;

			int swap = data[current];
			data[current] = data[current/2];
			data[current/2] = swap;
			current = current/2;
		}
	}

	public int remove(int target) {
		int answer = data[1]; //largest item (being removed)
		data[1] = data[last--];
		int current = 1;
		while(current*2 <= last) {

			int largerChild = current*2;

			if((largerChild < last) && (data[largerChild + 1] > data[largerChild]))
				largerChild++;

			if(data[current] > data[largerChild]) break;

			data[current] = data[largerChild];
			data[largerChild] = swap;
			current = largerChild;
		}

		return answer;
	}

}